﻿using EnvDTE;
using OD.ProjectCustomizationTool.Helpers;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;

namespace OD.ProjectCustomizationTool.Tools
{
    internal sealed class NoCodeAnalysisConfigurationRegister : ProjectUpdaterBase, IFlatFileUpdate, IEnvDteUpdate
    {
        protected override void InternalUpdate()
        {   // Nothing to do here.
        }

        /// <summary>
        /// Update the project and the solution files to consider code analysis and style cop status.
        /// </summary>
        void IFlatFileUpdate.Update()
        {
            using (var monitor = new FlatProjectFileUpdateMonitor(this))
            {
                var dwcaPropertyGroup = monitor.GetConditionnedProperty(Properties.Resources.DebugWithoutCodeAnalysisConfigurationName);

                this.Monitor.Logger.WriteLine("Declaring style cop and code analysis disabled for '" + Properties.Resources.DebugWithoutCodeAnalysisConfigurationName + "'");
                #region StyleCopEnabled definition

                var styleCopEnabledNode = (from element in dwcaPropertyGroup.Elements()
                                           where String.Equals(element.Name.LocalName, Properties.Resources.StyleCopEnabledProjectProperty)
                                           select element).FirstOrDefault();
                if (styleCopEnabledNode == null)
                {
                    styleCopEnabledNode = new XElement(XName.Get(Properties.Resources.StyleCopEnabledProjectProperty));
                    dwcaPropertyGroup.Add(styleCopEnabledNode);
                }
                styleCopEnabledNode.Value = false.ToString();

                #endregion
                #region RunCodeAnalysis definition

                var runCodeAnalysisNode = (from element in dwcaPropertyGroup.Elements()
                                           where String.Equals(element.Name.LocalName, Properties.Resources.RunCodeAnalysisProjectProperty)
                                           select element).FirstOrDefault();
                if (runCodeAnalysisNode == null)
                {
                    runCodeAnalysisNode = new XElement(XName.Get(Properties.Resources.RunCodeAnalysisProjectProperty));
                    dwcaPropertyGroup.Add(runCodeAnalysisNode);
                }
                runCodeAnalysisNode.Value = false.ToString();

                #endregion
            }
        }

        void IEnvDteUpdate.Update()
        {
            this.Monitor.Logger.WriteLine("Trying add configuration '" + Properties.Resources.DebugWithoutCodeAnalysisConfigurationName + "' to solution.");
            this.Monitor.Source.ConfigurationManager.AddConfigurationRow(
                Properties.Resources.DebugWithoutCodeAnalysisConfigurationName,
                Properties.Resources.DebugConfigurationName,
                true);
        }

    }
}
